<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Faces;

class FaceController extends Controller
{
    protected $fields = [
        'min' => '',
        'max' => '',
        'star' => '',
    ];

    public function index(Request $request){
        if ($request->ajax()){
            $data = array();
            $data['draw'] = $request->get('draw');
            $start = $request->get('start');
            $length = $request->get('length');
            $order = $request->get('order');
            $columns = $request->get('columns');
            $search = $request->get('search');
            $data['recordsTotal'] = Faces::count();
            if (strlen($search['value']) > 0) {
                $data['recordsFiltered'] = Faces::where(function ($query) use ($search) {
                    $query->where('star', 'LIKE', '%' . $search['value'] . '%');
                })->count();

                $data['data'] = Faces::where(function ($query) use ($search) {
                    $query->where('star', 'LIKE', '%' . $search['value'] . '%');
                })
                    ->skip($start)->take($length)
                    ->orderBy($columns[$order[0]['column']]['data'], $order[0]['dir'])
                    ->get();

            }else{
                $data['recordsFiltered'] = Faces::count();
                $data['data'] = Faces::
                skip($start)->take($length)
                    ->orderBy($columns[$order[0]['column']]['data'], $order[0]['dir'])
                    ->get();

            }

            return response()->json($data);
        }
       return view('admin.face.index');
    }

    public function create(){
        $data = [];
        foreach ($this->fields as $field => $default) {
            $data[$field] = old($field, $default);
        }
        return view('admin.face.create', $data);
    }

    public function store(Request $request)
    {

        $faces = new Faces();
        foreach (array_keys($this->fields) as $field) {
            $faces->$field = $request->get($field);
        }
        $faces->save();
        return redirect('/admin/face')->withSuccess('添加成功！');
    }

    public function edit($id)
    {
        $faces = Faces::find((int)$id);
        if (!$faces) return redirect('/admin/permission')->withErrors("找不到该等级!");
        $data = ['id' => (int)$id];
        foreach (array_keys($this->fields) as $field) {
            $data[$field] = old($field, $faces->$field);
        }
        return view('admin.face.edit', $data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param PermissionUpdateRequest|Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $faces = Faces::find((int)$id);
        foreach (array_keys($this->fields) as $field) {
            $faces->$field = $request->get($field);
        }
        $faces->save();
        return redirect('admin/face')->withSuccess('修改成功！');
    }

    public function destroy($id){
        $faces = Faces::find((int)$id);
        if ($faces) {
            $faces->delete();
        } else {
            return redirect()->back()
                ->withErrors("删除失败");
        }

        return redirect()->back()
            ->withSuccess("删除成功");
    }

}
